package cn.stylefeng.guns.modular.web.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.core.ParamUtil;
import cn.stylefeng.guns.modular.web.entity.CumulaDay;
import cn.stylefeng.guns.modular.web.service.ICumulaDayService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 描述：连续打卡额外奖励金币设置表 controller
 * 作者：chenhuiyan
 * 时间：2020-04-18
 */
@Controller
@RequestMapping("/web/cumulaDay")
public class CumulaDayController extends BaseController {

    private String PREFIX = "/modular/web/cumulaDay/";

    @Autowired
    private ICumulaDayService cumulaDayService;

    /**
     * 跳转到管理首页
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "cumulaDay.html";
    }

    /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list() {
        Map<String, Object> params = ParamUtil.getParams();
        Page<Map<String, Object>> list = cumulaDayService.list(params);
        return LayuiPageFactory.createPageInfo(list);
    }

    /**
     * 跳转添加/修改页面
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(String cumulaDayId, Model model) {
        model.addAttribute("cumulaDayId", cumulaDayId);
        return PREFIX + "cumulaDay_add.html";
    }

    /**
     * 保存
     * @param cumulaDay
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save")
    public ResponseData save(CumulaDay cumulaDay) {
        if (ToolUtil.isEmpty(cumulaDay.getId())) {
            //判断天数是否存在
            CumulaDay cd = cumulaDayService.getOne(new QueryWrapper<CumulaDay>()
                    .eq("day_num", cumulaDay.getDayNum())
                    .eq("del_flag", 0));
            if(ToolUtil.isNotEmpty(cd)){
                throw new ServiceException(BizExceptionEnum.EXISTED_CUMULA_DAY);
            }

            cumulaDay.preInsert();
            cumulaDayService.save(cumulaDay);
        }else{
            cumulaDay.preUpdate();
            cumulaDayService.updateById(cumulaDay);
        }
        return SUCCESS_TIP;
    }

    /**
     * 获取详情
     * @param cumulaDayId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInfoById/{cumulaDayId}")
    public Object getInfoById(@PathVariable("cumulaDayId") String cumulaDayId){
        CumulaDay cumulaDay = cumulaDayService.getById(cumulaDayId);
        return cumulaDay;
    }

    /**
     * 删除
     * @param cumulaDayId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public ResponseData delete(@RequestParam(name = "cumulaDayId") String cumulaDayId){
        if(ToolUtil.isEmpty(cumulaDayId)){
            throw new ServiceException(BizExceptionEnum.PARAM_ERROR);
        }
        CumulaDay cumulaDay = cumulaDayService.getById(cumulaDayId);
        cumulaDay.preUpdate();
        cumulaDay.setDelFlag(1);
        cumulaDayService.updateById(cumulaDay);
        return SUCCESS_TIP;
    }

}
